<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8" />
<head>
    <title>一对一聊天</title>
    <script th:src="@{sockjs.min.js}"></script>
    <script th:src="@{jquery-3.3.1.min.js}"></script>
    <script th:src="@{stomp.min.js}"></script>
</head>
<body>
    <p>聊天室</p>
    <form id="wiselyFrom">
        <textarea rows="4" cols="60" name="text"></textarea>
        <input type="submit" value="发送"/>
    </form>


<script th:inline="javascript">
    $('#wiselyFrom').submit(function (e) {
        e.preventDefault();
        var text = $('#wiselyFrom').find('textarea[name="text"]').val();
        sendSpittle(text);
    });

    var sock = new SockJS("endpointChat");
    var stomp = Stomp.over(sock);
    stomp.connect('guest','guest',function (frame) {
        stomp.subscribe("/user/queue/notifications",handleNotification);
    });

    function handleNotification(message) {
        $('#output').append("<b>Received:"+message.body+"</b><br/>")
    }

    function sendSpittle(text) {
        stomp.send("/chat",{},text);
    }
    $('#stop').click(function () {
        sock.close()
    });
</script>
<div id="output"></div>
</body>
</html>